package com.egreen.system.service.api.impl;

import com.egreen.system.repository.VerificationCodeRedisRepository;
import com.egreen.common.serviceapi.system.VerificationCodeAPI;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@DubboService
@Component
public class VerificationCodeAPIIMPl implements VerificationCodeAPI {
    @Autowired
    VerificationCodeRedisRepository codeRedisRepository;


    /**
     * 用于接收验证码，保存在redis中
     *
     * @param userId
     * @param code
     */
    @Override
    public void addVerificationCode(Long userId, String code) {
        //1.先判断验证码是否存在
        Boolean exist = codeRedisRepository.exist(userId);
        if (exist) {
            codeRedisRepository.deleteCode(userId);
        }

        //2.验证码不在redis中再添加
        codeRedisRepository.save(userId, code);

    }

    /**
     * 拿该用户的验证码
     *
     * @return
     */
    @Override
    public String getVerificationCode(Long userId) {
        String Code = codeRedisRepository.getKeyCode(userId);
        return Code;
    }

    /*String*/

    /**
     * 用于接收验证码，保存在redis中
     *
     * @param userKey
     * @param code
     */
    @Override
    public void addVerificationCode(String userKey, String code) {
        //1.先判断验证码是否存在
        Boolean exist = codeRedisRepository.exist(userKey);
        if (exist) {
            codeRedisRepository.deleteCode(userKey);
        }
        //2.验证码不在redis中再添加
        codeRedisRepository.save(userKey, code);
    }

    /**
     * 拿该用户的验证码
     *
     * @return
     */
    @Override
    public String getVerificationCode(String userKey) {
        String Code = codeRedisRepository.getKeyCode(userKey);
        return Code;
    }

}
